package rok3.projekt.zespver3.client.screens;

import static rok3.projekt.zespver3.client.Settings._window_height;
import static rok3.projekt.zespver3.client.Settings._window_width;

import java.io.IOException;

import rok3.projekt.zespver3.client.MyGame;
import rok3.projekt.zespver3.client.Resources;
import rok3.projekt.zespver3.client.Settings;
import rok3.projekt.zespver3.network.packet.__StartGameInfo;
import rok3.projekt.zespver3.utils.GameClientUpdater;

import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.Screen;
import com.badlogic.gdx.graphics.GL20;
import com.badlogic.gdx.scenes.scene2d.InputEvent;
import com.badlogic.gdx.scenes.scene2d.Stage;
import com.badlogic.gdx.scenes.scene2d.ui.Label;
import com.badlogic.gdx.scenes.scene2d.ui.List;
import com.badlogic.gdx.scenes.scene2d.ui.TextButton;
import com.badlogic.gdx.scenes.scene2d.utils.ClickListener;

public class RoomScreen implements Screen{
	private MyGame game;
	private GameClientUpdater updater;
	private Stage stage;
	public volatile boolean waitForStart=false;
	public volatile boolean readyForStart=false;
	private __StartGameInfo startGameInfo;
	

	private boolean ready;
	
	public RoomScreen(MyGame myGame) {
		this.game = myGame;
		this.updater = Resources._updater;
		System.out.println("TestRoom#constructor - OK");
	}
	
	@Override
	public void render(float delta) {
		Gdx.gl.glClearColor(0.5f, 0.5f, 0.5f, 1);
		Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
		
		if ( readyForStart ) {
			game.setScreen(new InGameScreen(game,startGameInfo));
		}
		stage.act(delta);
		stage.draw();
		
	}

	@Override
	public void resize(int width, int height) {
		
	}

	@Override
	public void show() {
		stage = new Stage(_window_width,_window_height,true);
		TextButton button_ready = new TextButton("Ready",Resources._skin);
		button_ready.setPosition(200, 200);
		
		TextButton button_start = new TextButton("Start",Resources._skin);
		button_start.setPosition(300, 200);
		
		final Label mess = new Label("", Resources._skin);
		mess.setPosition(100, 300);
		
		//String[] item = { "192.227.166.97 - marek", "194.29.146.42 - len" };
		final List list = new List(Settings._host_address, Resources._skin);
		list.setPosition(100, 100);
		
		TextButton connect = new TextButton("Connect", Resources._skin);
		connect.setPosition(100,200);
		connect.addListener(new ClickListener() {
			@Override
			public void clicked(InputEvent event, float x, float y) {
				Settings._host_selection = list.getSelectedIndex();
				try {
					Resources._updater.tryConnecting();
				} catch (IOException e) {
					// TODO Auto-generated catch block
					mess.setText(e.getMessage());
				}
			}
		});
		
		button_ready.addListener(new ClickListener() {

			@Override
			public void clicked(InputEvent event, float x, float y) {
				if (waitForStart ) return;
				if ( !updater.isConnected() ) {
					mess.setText("Not connected to server, select server then click Connect");
					return;
				} 
					updater.sendReadySignal();
					mess.setText("Ready. wait till others are ready");
					ready = true;
				
			}
		});
		
		button_start.addListener(new ClickListener() {
			@Override
			public void clicked(InputEvent event, float x, float y) {
				if (waitForStart ) return;
				if (!ready ) {
					mess.setText("Not ready, click Ready");
					return;
				}
				updater.sendStartSignal();
				waitForStart = true;
				mess.setText("Starting game. pls wait");
			}
		});
		
		stage.addActor(mess);
		stage.addActor(button_ready);
		stage.addActor(button_start);
		stage.addActor(list);
		stage.addActor(connect);
		Gdx.input.setInputProcessor(stage);
	}
	
	@Override
	public void hide() {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void pause() {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void resume() {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void dispose() {
		// TODO Auto-generated method stub
		
	}

	public void setStartGameInfo(__StartGameInfo sgi) {
		readyForStart = true;
		this.startGameInfo = sgi;
	}

}
